<?php
error_reporting(E_ERROR | E_CORE_ERROR);
// 初始化结果信息
$resultMessage = '';
$diceValues = [];
$totalPoints = 0;

// 处理表单提交
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $userGuess = $_POST['guess'];

    // 模拟掷三个骰子
    for ($i = 0; $i < 3; $i++) {
        $diceValues[] = rand(1, 6);
    }

    // 计算骰子点数总和
    $totalPoints = array_sum($diceValues);

    // 判断大小
    $isBig = $totalPoints >= 11 && $totalPoints <= 18;
    $isSmall = $totalPoints >= 3 && $totalPoints <= 10;

    // 判断玩家猜测结果
    if (($userGuess === 'big' && $isBig) || ($userGuess === 'small' && $isSmall)) {
        $resultMessage = "恭喜你，猜对啦！骰子点数分别为 " . implode(',', $diceValues) . "，总和为" .$totalPoints;
    } else {
        $resultMessage = "很遗憾，猜错了。骰子点数分别为 " . implode(',', $diceValues) . "，总和为" .$totalPoints;
    }
}
?>

<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>猜骰子大小游戏</title>
    <link rel="stylesheet" href="style.css">
</head>

<body>
    <h1>猜骰子大小游戏</h1>
    <form method="post">
        <label>
            <input type="radio" name="guess" value="big" required> 大（11 - 18 点）
        </label>
        <label>
            <input type="radio" name="guess" value="small" required> 小（3 - 10 点）
        </label>
        <input type="submit" value="猜测">
    </form>
    <?php if ($resultMessage): ?>
        <div id="result">
            <p><?php echo $resultMessage; ?></p>
        </div>
    <?php endif; ?>
</body>

</html>